
//
//  BezierView.m
//  室内地图测试
//
//  Created by Maybe_文仔 on 2018/6/6.
//  Copyright © 2018年 Maybe. All rights reserved.
//

#import "RoomBezierView.h"
#import "HYMeetingRoomModel.h"

@interface RoomBezierView()
@property (nonatomic, strong) NSMutableArray *pathInfoMArr;
@end

@implementation RoomBezierView

- (void)drawRect:(CGRect)rect {
    
    
    
    for (HYMeetingRoomModel *model in self.areaList) {
        
        if ([model.status intValue] == 1) {
            //设置房间可用填充颜色
            [KAPP_Available_COLOR setFill];
        }
        else{
            //设置房间见不可用填充颜色
            [KAPP_7b7b7b_COLOR setFill];
        }
        //创建UIBezierPath
        UIBezierPath *apath = ({
            
            UIBezierPath *path = [UIBezierPath bezierPath];
            path.lineWidth     = 0.0f;              //设置线条宽度
            path.lineCapStyle  = kCGLineCapRound;   //设置拐角
            path.lineJoinStyle = kCGLineCapRound;  //终点处理
            
            NSArray *areaArr = model.area;
            for (int i = 0; i < areaArr.count; i++) {
                NSDictionary *dic = areaArr[i];
                if (i == 0) {
                    //设置起始点
                    [path moveToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                    //添加区域名称
                    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake([dic[@"x"] floatValue], [dic[@"y"] floatValue], 60, 21)];
                    nameLab.text = model.name;
                    nameLab.textColor = [model.status intValue] == 1 ? KAPP_WHITE_COLOR : KAPP_BLACK_COLOR;
                    nameLab.font = KFont(15);
                    [self addSubview:nameLab];
                }
                else{
                     //增加线条
                    [path addLineToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                }
            }
        
            //关闭路径
            [path closePath];
            
            path;
        });
        
        model.path = apath;
        [self.pathInfoMArr addObject:model];
    
        //根据坐标连线
        [apath stroke];
        //根据闭合路线填充色彩
        [apath fill];
    }
    
}

-(void)setAreaList:(NSArray *)areaList{
    _areaList = areaList;
}

-(void)containsPoint:(CGPoint)point complete:(ConatinsPointBlock)hasConatainsBlock{
    for (HYMeetingRoomModel *model in self.pathInfoMArr) {
        
        if ([model.path containsPoint:point] && [model.status intValue] == 1) {
            hasConatainsBlock(model.id);
        }
    }
}

-(NSMutableArray *)pathInfoMArr{
    if (!_pathInfoMArr) {
        _pathInfoMArr = [NSMutableArray new];
    }
    return _pathInfoMArr;
}

@end
